package izzat.fit.dbservice.controller;

import izzat.fit.dbservice.entity.ResponseEntity;
import izzat.fit.dbservice.entity.action.Action;
import izzat.fit.dbservice.entity.training.ActionSet;
import izzat.fit.dbservice.entity.training.Plan;
import izzat.fit.dbservice.entity.training.RequestEntity;
import izzat.fit.dbservice.service.ActionService;
import izzat.fit.dbservice.service.TrainingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: TrainingDbServiceController
 * @Description: 训练数据库控制器
 * @Author: Izzat
 * @Date: 2020-01-16 14:11
 * @Version: v1.0
 */
@RestController
@RequestMapping("db-service/training")
public class TrainingDbServiceController {
    @Autowired
    private TrainingService trainingService;

    @RequestMapping("allPlan")
    /**
    * @Description:
    * @Param: []
    * @return: izzat.fit.dbservice.entity.ResponseEntity
    * @Author: Izzat
    * @Date: 2020/1/16  14:20
    */
    public ResponseEntity getAllPlan(){
        ResponseEntity response = new ResponseEntity();
        List<Plan> serchResult = trainingService.getAllPlan();
        String info;
        int size = serchResult.size();
        if(size > 0){
            info = "查询成功，为您找到" + size + "条记录";
            response.status(200).datas(serchResult).info(info);
        }else {
            info = "找不到相关记录";
            response.status(400).info(info);
        }
        return response;
    }

    @RequestMapping("findPlanById")
    /**
     * @Description: 按找Id查找计划
     * @Param: [id]
     * @return: izzat.fit.dbservice.entity.action.Action
     * @Author: Izzat
     * @Date: 2019/12/31  23:10
     */
    public ResponseEntity findPlanById(@RequestBody int id){
        ResponseEntity response = new ResponseEntity();
        List<Plan> serchResult = new ArrayList<>();
        serchResult.add(trainingService.findPlanById(id));
        String info;
        int size = serchResult.size();
        if(size > 0){
            info = "查询成功，为您找到" + size + "条记录";
            response.status(200).datas(serchResult).info(info);
        }else {
            info = "找不到相关记录";
            response.status(400).info(info);
        }
        return response;
    }

    @RequestMapping("findPlanByCreator")
    /** 
    * @Description: 按创建者查找 
    * @Param: [id] 
    * @return: izzat.fit.dbservice.entity.ResponseEntity 
    * @Author: Izzat 
    * @Date: 2020/1/25  13:13
    */ 
    public ResponseEntity findPlanByCreator(@RequestBody RequestEntity requestEntity){
        ResponseEntity response = new ResponseEntity();
        List<Plan> serchResult = trainingService.findPlanByCreator(requestEntity.getCreator(), requestEntity.getMode());
        String info;
        int size = serchResult.size();
        if(size > 0){
            info = "查询成功，为您找到" + size + "条记录";
            response.status(200).datas(serchResult).info(info);
        }else {
            info = "找不到相关记录";
            response.status(400).info(info);
        }
        return response;
    }

    @RequestMapping("deletePlanById")
    /**
    * @Description: 按找id删除计划
    * @Param: [id]
    * @return: izzat.fit.dbservice.entity.ResponseEntity
    * @Author: Izzat
    * @Date: 2020/1/27  22:36
    */
    public ResponseEntity deletePlanById(@RequestBody int[] deleteList){
        ResponseEntity response = new ResponseEntity();
        int deleteResult = trainingService.deletePlanById(deleteList);
        String info;
        int size = deleteList.length;
        if(size == deleteResult){
            info = "删除成功，已删除" + size + "条记录";
            response.status(200).info(info);
        }else {
            info = "未成功";
            response.status(400).info(info);
        }
        return response;
    }

    @RequestMapping("findActionSetByPlanId")
    /** 
    * @Description: 按找计划id找训练组
    * @Param: [id] 
    * @return: izzat.fit.dbservice.entity.ResponseEntity 
    * @Author: Izzat 
    * @Date: 2020/1/16  16:23
    */ 
    public ResponseEntity findActionSetByPlanId(@RequestBody int id){
        ResponseEntity response = new ResponseEntity();
        List<ActionSet> serchResult = trainingService.findActionSetByPlanId(id);
        String info;
        int size = serchResult.size();
        if(size > 0){
            info = "查询成功，为您找到" + size + "条记录";
            response.status(200).datas(serchResult).info(info);
        }else {
            info = "找不到相关记录";
            response.status(400).info(info);
        }
        return response;
    }
    

    @RequestMapping("addPlan")
    /**
    * @Description: 增加一个训练计划
    * @Param: [request]
    * @return: izzat.fit.dbservice.entity.ResponseEntity
    * @Author: Izzat
    * @Date: 2020/3/16  21:40
    */
    public ResponseEntity addPlan(@RequestBody RequestEntity request){
        ResponseEntity response = new ResponseEntity();
        int inserstResult = trainingService.addPlan(request.getPlan(), request.getActionSets());
        String info;
        if(inserstResult == 1){
            info = "添加成功，为您新增" + inserstResult + "条记录";
            response.status(200).info(info);
        }else {
            info = "添加失败";
            response.status(400).info(info);
        }
        return response;
    }
}
